/*!***************************************************
 * @file: app.h
 * @brief: 程序的总调度层
 * @author: ITHAO 
 * @date: 2018.11.19
 * @note: 
 ****************************************************/
#ifndef __APP_H__
#define __APP_H__

#include <setjmp.h>
#include <pthread.h>

#include "client_management.h"
#include "comm_protocol_core.h"

#define  APP_BLACK         "\033[30m"
#define  APP_RED           "\033[31m"
#define  APP_GREEN         "\033[32m"
#define  APP_YELLOW        "\033[33m"
#define  APP_BLUE          "\033[34m"
#define  APP_WHITE         "\033[37m"
#define  APP_NONE          "\033[0m"
#define  CLEAN_LINE         "\033[K"
#define  REVERSE_SHOW       "\033[7m"
#define  HIGH_LIGHT         "\033[?25h"
#define  LOW_LIGHT          "\033[?25l"


#define CLEAR()         printf("\033[2J")           /*清除屏幕*/
#define MOVEUP(x)       printf("\033[%dA", (x))     /*上移光标*/
#define MOVEDOWN(x)     printf("\033[%dB", (x))     /*下移光标*/
#define MOVELEFT(y)     printf("\033[%dD", (y))     /*左移光标*/
#define MOVERIGHT(y)    printf("\033[%dC",(y))      /*右移光标*/
#define MOVETO(x,y)     printf("\033[%d;%dH", (x), (y))     /*定位光标*/
#define RESET_CURSOR()  printf("\033[H")            /*光标复位*/
#define HIDE_CURSOR()   printf("\033[?25l")         /*隐藏光标*/
#define SHOW_CURSOR()   printf("\033[?25h")         /*显示光标*/
#define HIGHT_LIGHT()   printf("\033[7m")         
#define UN_HIGHT_LIGHT() printf("\033[27m")

#define MAX_SELECT_NUM  100
#define MAX_WORK_THREAD_NUM  5

enum {
    APP_PARAM_NULL = 1,
    APP_CREATE_PTHREAD_ERR,
    APP_SHMGET_ERR,
    APP_SHMAT_ERR,
    APP_CBUF_ALLOC_ERR,
};

/*所有缓冲区定义在这个结构体*/
struct cbufs {
    struct cbuf *to_recv_pthread_fd;/*用于给select线程传fd的buf*/
    struct cbuf *recv_data;/*系统接收需要处理的信息的buf*/
};

/*写入recv_data的结构体，就是在通信收到的结构体上加入对应用户的fd*/
struct data_with_fd {
    int clientfd;
    struct data_packet data;
};

/*所有线程定义在这里*/
struct server_pthreads {
    pthread_t recv_thread;/*接收信息的线程*/
    pthread_t work_thread[MAX_WORK_THREAD_NUM];/*工作线程*/
};

struct app {
    //jmp_buf ctrl_c_jump;/*用于在按下CTRL+C后跳转*/
    struct online_user online_user;/*在线用户*/
    struct cbufs cbufs;/*程序所用到的buffer*/
    struct server_pthreads server_pthreads;/*所有线程*/
};


extern int init(struct app *p_app);
extern int run(struct app *p_app);
extern int destroy(struct app *p_app);

#endif